try:
	from xml.etree import ElementTree as ET
except ImportError:
	from elementtree import ElementTree as ET
from os import popen, path

__all__ = ["version"]

_this_dir = path.dirname(path.realpath(__file__))

def _svn(*args):
	command = "svn %s --xml" % " ".join(map(lambda x: "'%s'" % x, args))
	fp = popen(command)
	output = ""
	while True:
		chunk = fp.read()
		if not chunk:
			break
		output = output + chunk
	fp.close()
	return output

def version(revision = "HEAD"):
	xml = _svn("info", "-r", revision, _this_dir)
	tree = ET.fromstring(xml)
	e = tree.find("entry")
	if e is not None and "revision" in e.attrib:
		return e.attrib["revision"]
	else:
		return "unknown"
